package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class testSQL {
    public static void main(String[] args){
/*        try {
            // 注册驱动
//            String driverName=;
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 连接数据库
            String url="jdbc:mysql://localhost:3306/javawebsql?useUnicode=true&characterEncoding=utf-8";
            Connection con = DriverManager.getConnection(url, "root", "123456");
            System.out.println(con);
            // 使用数据库
            Statement statement = con.createStatement();
            statement.execute("insert into test(UserName,Password) values('root1','123')");

            // 断开连接
            con.close();
            System.out.println("已完成");
        }catch(Exception e){
            System.out.println(e);
        }*/


        try {
            // 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 连接数据库
            String url="jdbc:mysql://localhost:3306/javawebsql?useUnicode=true&characterEncoding=utf-8";
            Connection con = DriverManager.getConnection(url, "root", "123456");
            System.out.println(con);
            // 使用数据库
            Statement statement = con.createStatement();
            ResultSet result = statement.executeQuery("select * from test");

//            System.out.println(result.next());
            while (result.next()){
                String name=result.getString("UserName");
                String pwd=result.getString("Password");
                System.out.println(name);
                System.out.println(pwd);
            }


            // 断开连接
            con.close();


        }catch(Exception e){
            System.out.println(e);
        }

    }
}
